const cv = require('opencv4nodejs');
//const posenet = require('./posenet')

const delay = 50;

const videoFile = 'rtmp://rtmp.open.ys7.com/openlive/48c4402b79214052abd80601c9f24a44'

async function run() {
    const bgSubtractor = new cv.BackgroundSubtractorKNN();

    //const net = await posenet.load(0.75);

    const cap = new cv.VideoCapture(videoFile);
    let done = false;
    const intvl = setInterval(() => {
        let frame = cap.read();
        // loop back to start on end of stream reached
        if (frame.empty) {
            cap.reset();
            frame = cap.read();
        }

        cv.imshow('frame', frame);


        const key = cv.waitKey(delay);
        done = key !== -1 && key !== 255;
        if (done) {
            clearInterval(intvl);
            console.log('Key pressed, exiting.');
        }
    }, 0);


    // grabFrames(, delay, (frame) => {
    //   const foreGroundMask = bgSubtractor.apply(frame);

    //   const iterations = 2;
    //   const dilated = foreGroundMask.dilate(
    //     cv.getStructuringElement(cv.MORPH_ELLIPSE, new cv.Size(4, 4)),
    //     new cv.Point(-1, -1),
    //     iterations
    //   );
    //   const blurred = dilated.blur(new cv.Size(10, 10));
    //   const thresholded = blurred.threshold(200, 255, cv.THRESH_BINARY);

    //   const minPxSize = 4000;
    //   drawRectAroundBlobs(thresholded, frame, minPxSize);

    //   //cv.imshow('foreGroundMask', foreGroundMask);
    //   //cv.imshow('thresholded', thresholded);
    //   cv.imshow('frame', frame);
    // });
}

run()